JSP Expression Language (EL) Tutorial

您所在的位置:网站首页 jsp if test or JSP Expression Language (EL) Tutorial

JSP Expression Language (EL) Tutorial

#JSP Expression Language (EL) Tutorial| 来源: 网络整理| 查看: 265

What is Expression Language (EL)?

Expression Language (EL) is mechanism that simplifies the accessibility of the data stored in Java bean component and other object like request, session and application, etc. There are many operators in JSP that are used in EL like arithmetic and logical operators to perform an expression. It was introduced in JSP 2.0

In this tutorial, you will learn-

JSP Syntax of Expression Language (EL) JSP If-else JSP Switch JSP For loop JSP While loop JSP Operators JSP Syntax of Expression Language (EL)

Syntax of EL :$(expression)

In JSP, whatever present in the braces gets evaluated at runtime sent to the output stream. The expression is a valid EL expression and it can be mixed with a static text and can be combined with other expression to form larger expression.

To get a better idea, on how expression works in JSP, we will see below example.

In this example, we will see how EL is used as an operator to add two numbers (1+2) and get the output respectively.

Guru JSP1 Expression is: ${1+2};

Explanation of Code:

Code Line 11: Expression Language (EL) is set where we are adding two numbers 1+2, hence it will give output as 3.

When you execute the above code, you will have the following Output.

Output:

JSP Expression Language (EL)

Expression is: 3 (As numbers 1+2 will be added and serve as an output)

Flow Control Statements:

JSP provides the power of Java to be embedded in the application. We can use all the APIs and building blocks of Java in JSP programming including control flow statements which include decision making and the loop statements.

There are two types of flow control statements described below;

Decision-Making statements Loop Statements

Decision-Making Statements:

Decision-making statement in JSP is based on whether the condition set is true or false. The statement will behave accordingly.

There are two types of decision-making statements described below:

If – else switch JSP If-else

“If else” statement is basic of all control flow statements, and it tells the program to execute the certain section of code only if the particular test evaluates to true.

This condition is used to test for more than one condition whether they are true or false.

If the first condition is true then “if block” is executed and if it is false then “else block” is executed

Syntax for if – else statement:

If(test condition) { //Block of statements } else { //Block of statements }

In this example,

We are going to test “if else” condition by taking variable and checking the value if the variable matches with what it is initialized:

Guru JSP2 Its February Any month other than February

Explanation of the code:

Code Line 10: Variable named month is initialized to 5 in the expression tags Code Line 11: In the EL tags there is “if condition” stating that if the month is equal 2 (condition is tested here as true or false) Code Line 12: If the condition is true that is the variable month is 2 then it will print to output stream Code Line 13-15: If the above if condition fails then it will move to else part for all other cases where the statement will print to the output stream and condition is closed

When you execute the above code, you will have the following Output.

Output:

JSP Expression Language (EL)

Since the month we have is 5 which is not equal to #2 (February). Hence, we have the output “Any month other than February” (month is mentioned as 5 hence else are executed) JSP Switch

The body of the switch statement is called as a “switch block”.

The switch case is used to check the number of possible execution paths. A switch can be used with all data types The switch statements contain more than one cases and one default case It evaluates the expression then executes all the statements following the matching case

Syntax for switch statement:

switch (operator) { Case 1: Block of statements break; Case 2: Block of statements break; case n: Block of statements break; default: Block of statements break; } Switch block begins with one parameter, which is the operator that needs to be passed and Then there are different cases which provides condition and whichever matches with the operator that case is executed.

In below example,

We have defined a variable week, and it is matched with the case whichever is true.In this case, week is 2 hence 2nd case is matched, and the output is Tuesday:

Guru JSP3

Explanation of Code:

Code Line 10: Variable named week is initialized to 2 in the expression tags Code Line 11: In the EL tags the switch case is started where week is passed as parameter Code Line 12 – 29: All the cases have been mentioned starting from case 0 to case 5 where the value of week parameter is matched with the cases, and accordingly the output is print. In this case, the value is 2 hence case 2 will be executed in this case. Here “out” is the class of JSP which writes the output stream for generated response and “println” is a method of that class. Code Line 30-32: If the above all cases fails then it will move to default part and will get executed, where the statement will print to the output stream and condition is closed

When you execute the above code, you will have the following Output.

Output:

JSP Expression Language (EL)

The output in this case is Tuesday as 2nd case is called.

Loop Statements

JSP For loop

It is used for iterating the elements for a certain condition, and it has three parameters.

Variable counter is initialized Condition till the loop has to be executed Counter has to be incremented

For loop Syntax:

For(inti=0;i or gt Test for greater than = or ge Test for greater than or equal && or and Test for logical AND || or or Test for logical OR ! or not Unary Boolean complement Empty Test for empty variable values

In this example,

We are declaring two variables num1 and num2 and then take a variable num3, where we use JSP operator + by to add num1 and num2 and get num3. Then we check a condition whether num3 is not equal to 0 by using JSP operators (!= , >) and Then take another variable num4 by multiplying two num1 and num2 we get num4.

These all numbers should be printed out as our output:

Guru JSP6 0){ int num4= num1*num2; out.println("Number 4 is " +num4); out.println("Number 3 is " +num3); }%>

Explanation of the code:

Code Line 10: Variable named num1 is initialized to 10 and num2 to 50 in the expression tags Code Line 11: The variable num3 is sum of of num1 and num2 where we are using addition operator Code Line 12-16: In the EL tags, we have used an OR condition which is logical operator and arithmetic operators to check whether num3 is greater than 0 in the if condition. The OR is used when any one condition is true in such case it will enter the “if case”where we are multiplying two numbers “num1” and “num2” and getting output in “num4″and it will print the output stream.

When you execute the above code, you will have the following Output.

Output:

JSP Expression Language (EL)

The first output is Number 4 is 500 (variable num4 which is num1*num2) The second output is Number 3 is 60 (variable num3 which num1+num2)

Summary:

JSP Expression Language (EL) makes it easy to access the application for the data stored in the javabeans components. It also allows create the expressions which are both arithmetic and logical. Within EL tags we can use integers. Floating point numbers, strings and Boolean values. In JSP we can also use loops and decision making statements using EL tags You Might Like: MVC Architecture in Java with JSP Application Design Example JSTL (JSP Standard Tag Library) Tutorial: Core & Custom Tags JSP Client HTTP Request & Server Response with Example Top 80 Spring MVC Interview Questions (2023) Top 50 JSP Interview Questions (2023)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3